<div class="bjui-pageHeader">
    <form id="pagerForm" data-toggle="ajaxsearch" action="${ctxPath}/system/cms/site?pid=2" method="post">
        <input type="hidden" name="pageSize" value="${model.pageSize!}">
        <input type="hidden" name="pageCurrent" value="${model.pageNum!}">
        <input type="hidden" id="initProvince" value="${pojo.provinceCode!}">
        <input type="hidden" id="initCity" value="${pojo.cityCode!}">
        <input type="hidden" id="siteType" value="${pojo.siteType!}">

        <div class="bjui-searchBar">
            <!--<label>站点状态:</label>
            <select name="status" data-toggle="selectpicker">
                @if(isEmpty(pojo.status)){
                <option value=""  selected>所有</option>
                <option value="0"> 关闭</option>
                <option value="1"> 正常 </option>
                @}else if(pojo.status == 0){
                <option value="">所有</option>
                <option value="0" selected> 关闭</option>
                <option value="1"> 正常 </option>
                @}else if(pojo.status == 1){
                <option value="">所有</option>
                <option value="0"> 关闭</option>
                <option value="1"  selected> 正常 </option>
                @}
            </select>&nbsp;
            <label>站点省份:</label>
            <select name="provinceCode" id="l_province" data-toggle="selectpicker">
                <option value="0">所有省份</option>
            </select>
            <label>站点城市:</label>
            <select name="cityCode" id="l_city" data-toggle="selectpicker">
                <option value="0">所有城市</option>
            </select>
            <label>站点类型:</label>
            <select name="siteType" data-toggle="selectpicker" id="s-siteType">

            </select>
            <button type="submit" class="btn-default" data-icon="search">查询</button>&nbsp;
            <label>静态化类型:</label>
            <select data-toggle="selectpicker" id="staticType">
                <option value="0" selected>PC端 + 移动端</option>
                <option value="1">移动端</option>
                <option value="2">PC端</option>
            </select>
            <a class="btn btn-orange" href="javascript:;" data-toggle="reloadsearch" data-clear-query="true" data-icon="undo">清空查询</a>
            &nbsp;-->
            <label>静态化类型:</label>
            <select data-toggle="selectpicker" id="staticType">
                <option value="0" selected>PC端 + 移动端</option>
                <option value="1">移动端</option>
                <option value="2">PC端</option>
            </select>
            @if(shiro.hasPermission("site:input")){
            <a href="${ctxPath}/system/cms/site/input" class="btn btn-green" data-icon="plus" data-toggle="dialog" data-maxable="false" data-resizable="true"  data-width="800" data-height="550" data-id="add-admn-user"  data-title="新增子站点">新增子站点</a>
            <!--<a href="${ctxPath}/system/cms/site/inputMore" class="btn btn-green" data-icon="plus" data-toggle="dialog" data-maxable="false" data-resizable="true"  data-width="400" data-height="150" data-id="add-more-admn-user"  data-title="批量新增子站点">批量新增子站点</a>-->
            <!--<a href="${ctxPath}/system/cms/site/initInputMore" class="btn btn-green" data-icon="plus" data-toggle="doajax" data-width="400" data-height="150" data-confirm-msg="初始化创建站点" data-title="初始化创建站点">初始化创建站点</a>-->
            @}
            @if(shiro.hasPermission("staticMore:admin")){
            <a href="JavaScript:;" class=" btn btn-blue" id="static_subsit" data-confirm-msg="批量初始化子站点" data-icon="plus">批量初始化子站点</a>
            @}
            @if(shiro.hasPermission("site:delete")){
            <!--<a href="${ctxPath}/system/cms/site/deleteMore" class="btn btn-red" data-icon="minus" data-toggle="doajax" data-confirm-msg="确定要删除未用的子站点（未添加城市的站点）？" data-title="删除未用站点">删除未用站点</a>-->
            @}
            <button type="button" style="display: none" class="btn-green" id="staticShow" data-on-load="doc_dialog_onLoad" data-on-close="doc_dialog_onClose" data-toggle="dialog" data-id="mydialog3" data-target="#doc-dialog-target" data-title="站点静态化监控">站点静态化监控</button></p>
            <!--<div class="pull-right">
                <button type="button" class="btn-blue" data-url="ajaxDone2.html?id={#bjui-selected}" data-toggle="doajax" data-confirm-msg="确定要删除选中项吗？" data-icon="remove" title="可以在控制台(network)查看被删除ID">删除选中行</button>&nbsp;
                <div class="btn-group">
                    <button type="button" class="btn-default dropdown-toggle" data-toggle="dropdown" data-icon="copy">复选框-批量操作<span class="caret"></span></button>
                    <ul class="dropdown-menu right" role="menu">
                        <li><a href="book1.xlsx" data-toggle="doexport" data-confirm-msg="确定要导出信息吗？">导出<span style="color: green;">当前页</span></a></li>
                        <li class="divider"></li>
                        <li><a href="${ctxPath}/system/toStaticHtml/bathStatic" data-toggle="doajaxchecked" data-confirm-msg="批量静态化子站点？" data-idname="ids" data-group="ids">批量静态化子站点</a></li>
                        <li><a href="${ctxPath}/system/cms/site/delete" data-toggle="doajaxchecked" data-confirm-msg="确定要删除选中项吗？" data-idname="ids" data-group="ids">删除<font COLOR="#ff6347">选中</font>行</a></li>
                        <li><a href="ajaxDone2.html" data-toggle="doajaxchecked" data-confirm-msg="确定要删除选中项吗？" data-idname="delids" data-group="ids">删除选中</a></li>
                    </ul>
                </div>
            </div>-->
        </div>
    </form>
</div>
<div class="bjui-pageContent tableContent">
    <table class="table table-bordered table-hover table-striped table-top" data-selected-multi="true" >
        <thead>
        <tr>
            <th align="center"  width="25">编号</th>
            <th align="center"  width="100">站点名称</th>
            <th align="center" width="100">当地域名</th>
            <th align="center"  width="60">城市</th>
            <th align="center"  width="60">站点类型</th>
            <!--<th align="center"  width="80">模板目录</th>
            <th align="center"  width="60">站点标题</th>
            <th align="center" width="80">关键字</th>
            <th align="center"  width="100">描述</th>-->
            <th align="center"  width="60">静态化状态</th>
            <th align="center"  width="60">站点排序</th>
            <th align="center" width="26" ><input type="checkbox" class="checkboxCtrl" data-group="ids" data-toggle="icheck"></th>
            <th align="center"  width="80">操作</th>
        </tr>
        </thead>
        <tbody id="data-table">
        @for(site in model.list){
        <tr data-id="${siteLP.index}" >
            <td align="center">${site.siteId!}</td>
            <td align="center"><a TARGET="_blank" HREF="${frontPath}/${site.siteId!}">${site.siteName!,xss}</a></td>
            <td align="center"><a HREF="http://${site.domain!,xss}">${site.domain!,xss}</a></td>
            <td align="center">${site.provinceCityName!"N/A",xss}</td>
            <td align="center">${site.siteTypeName!"N/A",xss}</td>
            <!--<td align="center">${site.template!"N/A",xss}</td>
            <td align="center">${site.title!"N/A",xss}</td>
            <td align="center">${site.keyword!"N/A",xss}</td>
            <td align="center">${site.description!"N/A",xss}</td>-->
            <td align="center">
                @if(site.staticStatus == 1){
                <span class="green">初始化成功</span>
                @} else if(site.staticStatus == 2){
                <span class="red">初始化失败</span>
                @} else {
                <span class="blue">尚未初始化</span>
                @}
            </td>
            <td align="center">${site.sortId!"N/A",xss}</td>
            <td align="center"><input type="checkbox" name="ids" data-toggle="icheck" value="${site.siteId!}"></td>
            <td align="center">
                @if(session.SysUser.siteId==site.siteId){
                <a class="btn btn-red" title="当前站点">当</a>
                @}else{
                <a href="${ctxPath}/system/cms/site/change?id=${site.siteId!}" class="btn btn-blue" data-toggle="doajax" title="切换站点" data-confirm-msg="确定要切换到[${site.siteName!,xss}]？">切</a>
                @}
                <a href="${ctxPath}/system/cms/site/input?id=${site.siteId!}" class="btn btn-green"  data-toggle="dialog" title="编辑站点" data-maxable="false" data-resizable="false"  data-width="800" data-height="550" data-id="edit-site-tab"  data-title="站点修改">编辑</a>
                <!--<a href="${ctxPath}/system/cms/site/delete?ids=${site.siteId!}" class="btn btn-red" data-toggle="doajax" title="删除站点" data-confirm-msg="确定要删除该行信息吗？">删</a>-->
            </td>
        </tr>
        @}
        </tbody>
    </table>
</div>
<div class="bjui-pageFooter">
    <div class="pages">
        <span>每页&nbsp;</span>
        <div class="selectPagesize">
            <select data-toggle="selectpicker" data-toggle-change="changepagesize">
                <option value="30">30</option>
                <option value="60">60</option>
                <option value="120">120</option>
                <option value="150">150</option>
            </select>
        </div>
        <span>&nbsp;条，共 ${model.total!} 条</span>
    </div>
    <div class="pagination-box" data-toggle="pagination" data-total="${model.total!}" data-page-size="30" data-page-current="1">
    </div>
</div>
<div id="doc-dialog-target" data-noinit="true" class="hide">
    <h4 style="text-align: center">站点初始化监控</h4>
    <p style="margin:10px">初始化站点总数:<span id="all_size"></span></p>
    <p style="margin:10px;color: #00B83F">初始化成功站点数:<span id="success_size">0</span></p>
    <p style="margin:10px;color:red">初始化失败站点数:<span id="error_size">0</span></p>
    <div id="site_list_error">

    </div>
</div>

<script type="text/javascript">


    var initProvince = $("#initProvince").val();
    var initCity = $("#initCity").val();
    var siteType = $("#siteType").val();
    var alaxCode = false,status;
    getSiteTypeList();

    getProviceList(true, "#l_province", "#l_city", initProvince, initCity);
    $("#l_province").change(function () {
        var provinceCode = this.value;
        getCityListByProvice(provinceCode, true, "#l_city", null);
    });

    var siteIds = [];    //定义一个空数组
    $("#static_subsit").click(function(){
        if(confirm('确定要静态化吗？') == false){
            return;
        }

        siteIds = [];
        var $chkBoxes = $('#data-table').find('input:checked');   //找到被选中的checkbox集
        if ($chkBoxes.length == 0) {         //如果不勾选弹出警告框
            console.log($chkBoxes.length)
            alert('请至少选择一个站点');
            return false;
        }

        //遍历被选中的checkbox集
        $($chkBoxes).each(function () {
            siteIds.push( $(this).val() );
        });


        $("#all_size").html(siteIds.length);
        $("#staticShow").click();
        var staticType = $("#staticType option:checked").val();

        $.ajax({
            type: "POST",
            url: "${ctxPath}/system/toStaticHtml/bathStatic?staticType=" + staticType,
            dataType : "json",
            contentType : 'application/json',
            data: JSON.stringify(siteIds),
            success: function(data){

                if (data.statusCode == "200") {
                    $("#static_subsit").alertmsg('correct', '静态化完成！')
                } else {
                    $("#static_subsit").alertmsg('correct', '静态化失败！')
                }

            }
        });

    })

    function doc_dialog_onLoad() {
        status  = setInterval(getAllSiteStatus, 5000);
    }

    function doc_dialog_onClose() {
        clearInterval(status);
    }

    getAllSiteStatus();

    function getAllSiteStatus(){

        if(siteIds.length == 0){
            return;
        }
        $.ajax({
            type: 'POST',
            url: "${ctxPath}/system/cms/site/findSite",
            dataType : "json",
            contentType : 'application/json',
            data: JSON.stringify(siteIds),
            success: function(data) {

                var successSiteLength = parseInt(data.successSite.length);
                var errorSiteLength = parseInt(data.errorSite.length);
                $("#success_size").html(successSiteLength);
                $("#error_size").html(errorSiteLength);
                var errorSite = data.errorSite;
                $("#site_list_error").empty();
                errorSite.forEach(function(value,i){
                    $("#site_list_error").append("<span class='error_site red'>"+ errorSite[i].siteName +"</span>");
                })
            },
            timeout: 5000,
            error: function (error) {
                console.log('失败啦');
            }

        });
    }


    /**
     * 根据父id查询站点类型列表
     */
    function getSiteTypeList() {
        var url =  "${ctxPath}/siteCode/listByPid";
        $.post(url,{pid:2},function(data){
            console.info(data)
            if(data.code == 0){
                var html = "<option value='0'>所有</option>";
                var list = data.result;
                for(var i=0; i < list.length; i++){
                    if(siteType != null && list[i].code == siteType){
                        html += "<option value='"+list[i].code+"' selected>"+list[i].description+"</option>";
                    }else{
                        html += "<option value='"+list[i].code+"'>"+list[i].description+"</option>";
                    }
                }
                $("#s-siteType").html(html);
                $("#s-siteType").selectpicker("refresh");
            }
        },"json");
    }

    /**
     * 获取省份列表
     * hasAll 是否含有全部
     * provinceElem 省份元素
     * cityElem 城市元素
     * initProvince 省份初始值
     * initCity 城市初始值
     */
    function getProviceList(hasAll, provinceElem, cityElem, initProvince, initCity){
        var url =  "${ctxPath}/city/getProviceList";
        $.post(url,null,function(data){
            var html = "";
            if(hasAll){
                html += "<option value='0'>所有省份</option>";
            }
            var list = data.result;
            for(var i=0; i < list.length; i++){
                if(initProvince != null && list[i].cityCode == initProvince){
                    html += "<option value='"+list[i].cityCode+"' selected>"+list[i].cityName+"</option>";
                    getCityListByProvice(list[i].cityCode, hasAll, cityElem, initCity);
                }else{
                    html += "<option value='"+list[i].cityCode+"'>"+list[i].cityName+"</option>";
                }
                if(!hasAll && initCity == null && i == 0){
                    getCityListByProvice(list[i].cityCode, hasAll, cityElem, initCity);
                }
            }
            $(provinceElem).html(html);
            $(provinceElem).selectpicker("refresh");
        },"json");
    };

    /**
     * 获取城市列表
     * hasAll 是否含有全部
     * provinceCode 省份code
     * cityElem 城市元素
     * initCity 城市初始值
     */
    function getCityListByProvice(provinceCode, hasAll, cityElem, initCity){
        var url = "${ctxPath}/city/getCityListByProvice";
        $.post(url,{provinceCode:provinceCode},function(data){
            var html = "";
            if(hasAll){
                html += "<option value='0'>所有城市</option>";
            }
            var list = data.result;
            for(var i=0; i < list.length; i++){
                if(initCity != null && list[i].cityCode == initCity){
                    html += "<option value='"+list[i].cityCode+"' selected>"+list[i].cityName+"</option>";
                }else{
                    html += "<option value='"+list[i].cityCode+"'>"+list[i].cityName+"</option>";
                }
            }
            $(cityElem).html(html);
            $(cityElem).selectpicker("refresh");
        },"json");
    };


</script>